package com.innovation.chengzhangriji.ui.widget;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;

import com.innovation.chengzhangriji.R;
import com.innovation.chengzhangriji.util.ChZhRJUtils;

import java.util.ArrayList;

public class DiscoverAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements DiscoverViewHolder.OnClickFriendListener, DiscoverAudioViewHolder.AudioListener, DiscoverPhotoViewHolder.OnClickPhotoListener, DiscoverVideoViewHolder.OnClickVideoListener, DiscoverProgressViewHolder.OnClickErrorListener {

    public interface Observer {

        int length();

        int type(int position);

        String userName(int position);

        String userUrl(int position);

        String babyName(int position);

        String babyUrl(int position);

        int babySex(int position);

        String time(int position);

        boolean showBottom(int position);
    }

    public interface TextObserver {

        String text(int position);

    }

    public interface PhotoObserver {

        void onClickPhoto(int position1, int position2);

        String photoLabel(int position);

        String photoContent(int position);

        String[] photoUrl(int position);

    }

    public interface AudioObserver {

        void onClickAudio(int position);

        int audioLength(int position);

        boolean audioPlay(int position);

        boolean audioShow(int position);

        boolean audioDownloading(int position);

        boolean audioDownloadSucceed(int position);

        void downloadAudio(int position);
    }

    public interface VideoObserver {

        void onClickVideo(int position);

        String thumbnailUrl(int position);

    }

    public interface MilestoneObserver {

        String milestone(int position);

    }

    public interface LikeObserver {

        void onClickLike(int position);

        boolean isLike(int position);

        int likeNumber(int position);

        boolean showLike(int position);

        ArrayList<String> likeList(int position);
    }

    public interface CommentObserver {

        void onClickComment(int position);

        void onClickComment(int position1, int position2);

        int commentNumber(int position);

        boolean showComment(int position);

        ArrayList<CommentModel> commentList(int position);

    }

    public interface OnClickErrorListener {

        void onClickError();

    }

    public static final int TEXT = 0;

    public static final int PHOTO = 1;

    public static final int AUDIO = 2;

    public static final int VIDEO = 3;

    public static final int MILESTONE = 4;

    private static final int LOAD_MORE = 5;

    private Context context;

    private Observer observer;

    private TextObserver textObserver;

    private PhotoObserver photoObserver;

    private AudioObserver audioObserver;

    private VideoObserver videoObserver;

    private MilestoneObserver milestoneObserver;

    private LikeObserver likeObserver;

    private CommentObserver commentObserver;

    private OnClickErrorListener onClickErrorListener;

    private boolean end;

    private boolean error;

    private DiscoverViewHolder mHolder = null;

    public DiscoverAdapter(Context context) {
        this.context = context;
        setHasStableIds(true);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view;
        RecyclerView.ViewHolder holder = null;
        switch (viewType) {
            case TEXT:
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_discover, parent, false);
                holder = new DiscoverTextViewHolder(view);
                break;
            case PHOTO:
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_discover, parent, false);
                holder = new DiscoverPhotoViewHolder(view);
                ((DiscoverPhotoViewHolder) holder).setOnClickPhotoListener(this);
                break;
            case AUDIO:
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_discover, parent, false);
                holder = new DiscoverAudioViewHolder(view);
                ((DiscoverAudioViewHolder) holder).setAudioListener(this);
                break;
            case VIDEO:
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_discover, parent, false);
                holder = new DiscoverVideoViewHolder(view);
                ((DiscoverVideoViewHolder) holder).setOnClickVideoListener(this);
                break;
            case MILESTONE:
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_discover, parent, false);
                holder = new DiscoverMilestoneViewHolder(view);
                break;
            case LOAD_MORE:
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_discover_progress, parent, false);
                holder = new DiscoverProgressViewHolder(view);
                ((DiscoverProgressViewHolder) holder).setOnClickErrorListener(this);
                return holder;
        }
        if (holder == null) {
            return null;
        }
        ((DiscoverViewHolder) holder).setOnClickFriendListener(this);
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (getItemViewType(position) == LOAD_MORE) {
            ((DiscoverProgressViewHolder) holder).setError(error);
        } else {
            ((DiscoverViewHolder) holder).setHideDialog();
            ((DiscoverViewHolder) holder).setUserName(observer.userName(position));
            ((DiscoverViewHolder) holder).setImageUserHeadUrl(observer.userUrl(position));
            ((DiscoverViewHolder) holder).setBabyName(observer.babyName(position));
            ((DiscoverViewHolder) holder).setBabyHeadUrl(observer.babyUrl(position), observer.babySex(position));
            ((DiscoverViewHolder) holder).setTime(observer.time(position));
            ((DiscoverViewHolder) holder).setBottomVisibility(observer.showBottom(position));
            ((DiscoverViewHolder) holder).setIsLike(context, likeObserver.isLike(position));
            ((DiscoverViewHolder) holder).setLikeNumber(likeObserver.likeNumber(position));
            ((DiscoverViewHolder) holder).setLikeVisibility(likeObserver.showLike(position));
            ((DiscoverViewHolder) holder).setCommentVisibility(commentObserver.showComment(position));
            ((DiscoverViewHolder) holder).setLikeList(likeObserver.likeList(position));
            ((DiscoverViewHolder) holder).setCommentNumber(commentObserver.commentNumber(position));
            ((DiscoverViewHolder) holder).setCommentList(context, commentObserver.commentList(position));
            switch (getItemViewType(position)) {
                case TEXT:
                    ((DiscoverTextViewHolder) holder).setText(textObserver.text(position));
                    break;
                case PHOTO:
                    ((DiscoverPhotoViewHolder) holder).setPhotoLabel(photoObserver.photoLabel(position));
                    ((DiscoverPhotoViewHolder) holder).setPhotoContent(photoObserver.photoContent(position));
                    ((DiscoverPhotoViewHolder) holder).setPhotoUrl(photoObserver.photoUrl(position));
                    break;
                case AUDIO:
                    ((DiscoverAudioViewHolder) holder).setAudioLength(audioObserver.audioLength(position));
                    ((DiscoverAudioViewHolder) holder).setAudioPlay(audioObserver.audioPlay(position));
                    ((DiscoverAudioViewHolder) holder).setAudioShow(audioObserver.audioShow(position));
                    ((DiscoverAudioViewHolder) holder).setAudioDownloading(audioObserver.audioDownloading(position));
                    ((DiscoverAudioViewHolder) holder).setAudioDownloadSucceed(audioObserver.audioDownloadSucceed(position));
                    ((DiscoverAudioViewHolder) holder).downloadAudio();
                    break;
                case VIDEO:
                    ((DiscoverVideoViewHolder) holder).setVideoThumbnailUrl(videoObserver.thumbnailUrl(position));
                    break;
                case MILESTONE:
                    ((DiscoverMilestoneViewHolder) holder).setMilestone(milestoneObserver.milestone(position));
                    break;
            }
            int paddingTop = (int) ChZhRJUtils.dp2px(14, holder.itemView.getResources().getDisplayMetrics());
            int paddingLeft = (int) ChZhRJUtils.dp2px(17, holder.itemView.getResources().getDisplayMetrics());
            if(likeObserver.showLike(position) || commentObserver.showComment(position)) {
                holder.itemView.setPadding(paddingLeft, paddingTop, paddingLeft, paddingTop);
            } else {
                int paddingBottom = (int) ChZhRJUtils.dp2px(3.5f, holder.itemView.getResources().getDisplayMetrics());
                holder.itemView.setPadding(paddingLeft, paddingTop, paddingLeft, paddingBottom);
            }
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (end) {
            return observer.type(position);
        } else {
            if (position == observer.length()) {
                return LOAD_MORE;
            } else {
                return observer.type(position);
            }
        }
    }

    @Override
    public int getItemCount() {
        if (observer.length() == 0) {
            end = true;
        }
        if (end) {
            return observer.length();
        } else {
            return observer.length() + 1;
        }
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public void onClickLike(DiscoverViewHolder holder) {
        likeObserver.onClickLike(holder.getLayoutPosition());
    }

    @Override
    public void onClickComment(DiscoverViewHolder holder) {
        commentObserver.onClickComment(holder.getLayoutPosition());
    }

    @Override
    public void onClickCommentPosition(DiscoverViewHolder holder, int position) {
        commentObserver.onClickComment(holder.getLayoutPosition(), position);
    }

    @Override
    public void onClickShowDialog(DiscoverViewHolder holder) {
        if(mHolder != null && holder != mHolder) {
            notifyItemChanged(mHolder.getLayoutPosition());
        }
        mHolder = holder;
    }

    @Override
    public void onClickHideDialog() {
        if(mHolder != null) {
            notifyItemChanged(mHolder.getLayoutPosition());
        }
    }

    @Override
    public void onClickPhoto(DiscoverPhotoViewHolder holder, int position) {
        photoObserver.onClickPhoto(holder.getLayoutPosition(), position);
    }

    @Override
    public void onClickAudio(DiscoverAudioViewHolder holder) {
        audioObserver.onClickAudio(holder.getLayoutPosition());
    }

    @Override
    public void downloadAudio(DiscoverAudioViewHolder holder) {
        audioObserver.downloadAudio(holder.getLayoutPosition());
    }

    @Override
    public void onClickVideo(DiscoverVideoViewHolder holder) {
        videoObserver.onClickVideo(holder.getLayoutPosition());
    }

    @Override
    public void onClickError() {
        if (isError()) {
            onClickErrorListener.onClickError();
        }
    }

    public void setObserver(Observer observer) {
        this.observer = observer;
    }

    public void setTextObserver(TextObserver textObserver) {
        this.textObserver = textObserver;
    }

    public void setPhotoObserver(PhotoObserver photoObserver) {
        this.photoObserver = photoObserver;
    }

    public void setAudioObserver(AudioObserver audioObserver) {
        this.audioObserver = audioObserver;
    }

    public void setVideoObserver(VideoObserver videoObserver) {
        this.videoObserver = videoObserver;
    }

    public void setMilestoneObserver(MilestoneObserver milestoneObserver) {
        this.milestoneObserver = milestoneObserver;
    }

    public void setLikeObserver(LikeObserver likeObserver) {
        this.likeObserver = likeObserver;
    }

    public void setCommentObserver(CommentObserver commentObserver) {
        this.commentObserver = commentObserver;
    }

    public void setOnClickErrorListener(OnClickErrorListener onClickErrorListener) {
        this.onClickErrorListener = onClickErrorListener;
    }

    public boolean isEnd() {
        return end;
    }

    public void setEnd(boolean end) {
        this.end = end;
    }

    public boolean isError() {
        return error;
    }

    public void setError(boolean error) {
        this.error = error;
    }

    public static class CommentModel {

        private String userName1;

        private String userName2;

        private String content;

        public String getUserName1() {
            return userName1;
        }

        public void setUserName1(String userName1) {
            this.userName1 = userName1;
        }

        public String getUserName2() {
            return userName2;
        }

        public void setUserName2(String userName2) {
            this.userName2 = userName2;
        }

        public String getContent() {
            return content;
        }

        public void setContent(String content) {
            this.content = content;
        }
    }
}
